/**
 * 
 */
package gwtool.gui.dataSetTable;

import gwtool.container.DataSet;
import gwtool.container.Gruppe;
import gwtool.container.PruefungAnzeigen;
import gwtool.gui.components.JComboCheckBox;
import gwtool.gui.dataSetTable.cellRenderer.DataSetCellRenderer;
import gwtool.gui.dataSetTable.cellRenderer.EinsatzstatusCellRenderer;
import gwtool.gui.dataSetTable.cellRenderer.PruefungCellRenderer;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import javax.swing.table.TableCellRenderer;

/**
 * @author Christian
 * 
 */
public class DataSetTablePanel extends JPanel {

	private JTextField filterTextField;

	private JComboBox<Gruppe> gruppenComboBox;

	private JTable dataSetTable;

	private DataSetTableController controller;

	private DataSetTabelModel tableModel = null;

	private JButton commitButton;

	private JButton delButton;

	private JButton neuButton;

	private JButton bearbeitenButton;

	private JPanel panel;

	private JLabel lblGruppe;

	private JComboBox<JCheckBox> typeComboBox;

	private JLabel lblType;
	private JButton btnDrucken;

	public DataSetTablePanel() {

		this.controller = new DataSetTableController(this);
		this.tableModel = controller.getModel();
		this.controller.addTableMouseListner();
		initialize();
	}

	private void initialize() {

		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] { 0, 0, 0, 0, 0 };
		gridBagLayout.rowHeights = new int[] { 0, 0, 30, 0 };
		gridBagLayout.columnWeights = new double[] { 1.0, 1.0, 0.0, 1.0, Double.MIN_VALUE };
		gridBagLayout.rowWeights = new double[] { 0.0, 1.0, 0.0, Double.MIN_VALUE };
		setLayout(gridBagLayout);

		panel = new JPanel();
		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.gridwidth = 4;
		gbc_panel.insets = new Insets(0, 0, 5, 5);
		gbc_panel.fill = GridBagConstraints.BOTH;
		gbc_panel.gridx = 0;
		gbc_panel.gridy = 0;
		add(panel, gbc_panel);
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.columnWidths = new int[] { 35, 28, 40, 0, 57, 86, 0 };
		gbl_panel.rowHeights = new int[] { 20, 0 };
		gbl_panel.columnWeights = new double[] { 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, Double.MIN_VALUE };
		gbl_panel.rowWeights = new double[] { 0.0, Double.MIN_VALUE };
		panel.setLayout(gbl_panel);
		lblGruppe = new JLabel("Gruppe");
		GridBagConstraints gbc_lblGruppe = new GridBagConstraints();
		gbc_lblGruppe.anchor = GridBagConstraints.WEST;
		gbc_lblGruppe.insets = new Insets(0, 5, 0, 5);
		gbc_lblGruppe.gridx = 0;
		gbc_lblGruppe.gridy = 0;
		panel.add(lblGruppe, gbc_lblGruppe);

		GridBagConstraints gbc_gruppenComboBox = new GridBagConstraints();
		gbc_gruppenComboBox.fill = GridBagConstraints.HORIZONTAL;
		gbc_gruppenComboBox.insets = new Insets(0, 0, 0, 5);
		gbc_gruppenComboBox.gridx = 1;
		gbc_gruppenComboBox.gridy = 0;
		panel.add(getGruppenComboBox(), gbc_gruppenComboBox);

		lblType = new JLabel("Type");
		GridBagConstraints gbc_lblType = new GridBagConstraints();
		gbc_lblType.insets = new Insets(0, 0, 0, 5);
		gbc_lblType.anchor = GridBagConstraints.EAST;
		gbc_lblType.gridx = 2;
		gbc_lblType.gridy = 0;
		panel.add(lblType, gbc_lblType);
		GridBagConstraints gbc_typeComboBox = new GridBagConstraints();
		gbc_typeComboBox.insets = new Insets(0, 0, 0, 5);
		gbc_typeComboBox.fill = GridBagConstraints.HORIZONTAL;
		gbc_typeComboBox.gridx = 3;
		gbc_typeComboBox.gridy = 0;
		panel.add(getTypeComboBox(), gbc_typeComboBox);
		JLabel lblFilter = new JLabel("Filter:");
		GridBagConstraints gbc_lblFilter = new GridBagConstraints();
		gbc_lblFilter.anchor = GridBagConstraints.EAST;
		gbc_lblFilter.insets = new Insets(0, 0, 0, 5);
		gbc_lblFilter.gridx = 4;
		gbc_lblFilter.gridy = 0;
		panel.add(lblFilter, gbc_lblFilter);
		filterTextField = new JTextField();
		GridBagConstraints gbc_filterTextField = new GridBagConstraints();
		gbc_filterTextField.fill = GridBagConstraints.HORIZONTAL;
		gbc_filterTextField.gridx = 5;
		gbc_filterTextField.gridy = 0;
		panel.add(filterTextField, gbc_filterTextField);
		filterTextField.addKeyListener(new KeyListener() {

			@Override
			public void keyTyped(KeyEvent arg0) {

				tableModel.aktualisiere();
			}

			@Override
			public void keyReleased(KeyEvent arg0) {

				tableModel.aktualisiere();
			}

			@Override
			public void keyPressed(KeyEvent arg0) {

				tableModel.aktualisiere();
			}
		});
		filterTextField.setColumns(10);
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.gridwidth = 4;
		gbc_scrollPane.insets = new Insets(0, 0, 5, 0);
		gbc_scrollPane.gridx = 0;
		gbc_scrollPane.gridy = 1;
		add(scrollPane, gbc_scrollPane);
		scrollPane.setViewportView(getDataSetTable());
		JPanel buttonPanel = new JPanel();
		GridBagConstraints gbc_buttonPanel = new GridBagConstraints();
		gbc_buttonPanel.gridwidth = 4;
		gbc_buttonPanel.fill = GridBagConstraints.BOTH;
		gbc_buttonPanel.gridx = 0;
		gbc_buttonPanel.gridy = 2;
		add(buttonPanel, gbc_buttonPanel);
		GridBagLayout gbl_buttonPanel = new GridBagLayout();
		gbl_buttonPanel.columnWidths = new int[] { 0, 0, 0, 0, 0, 0 };
		gbl_buttonPanel.rowHeights = new int[] { 0, 0 };
		gbl_buttonPanel.columnWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
		gbl_buttonPanel.rowWeights = new double[] { 0.0, Double.MIN_VALUE };
		buttonPanel.setLayout(gbl_buttonPanel);
		commitButton = new JButton("Infos hochladen");
		commitButton.addActionListener(controller);
		GridBagConstraints gbc_commitButton = new GridBagConstraints();
		gbc_commitButton.fill = GridBagConstraints.HORIZONTAL;
		gbc_commitButton.insets = new Insets(0, 5, 0, 5);
		gbc_commitButton.gridx = 0;
		gbc_commitButton.gridy = 0;
		buttonPanel.add(commitButton, gbc_commitButton);
		delButton = new JButton("L\u00F6schen");
		delButton.addActionListener(controller);
		GridBagConstraints gbc_delButton = new GridBagConstraints();
		gbc_delButton.fill = GridBagConstraints.HORIZONTAL;
		gbc_delButton.insets = new Insets(0, 0, 0, 5);
		gbc_delButton.gridx = 1;
		gbc_delButton.gridy = 0;
		buttonPanel.add(delButton, gbc_delButton);
		neuButton = new JButton("Neu");
		neuButton.addActionListener(controller);
		GridBagConstraints gbc_neuButton = new GridBagConstraints();
		gbc_neuButton.fill = GridBagConstraints.HORIZONTAL;
		gbc_neuButton.insets = new Insets(0, 0, 0, 5);
		gbc_neuButton.gridx = 2;
		gbc_neuButton.gridy = 0;
		buttonPanel.add(neuButton, gbc_neuButton);
		bearbeitenButton = new JButton("Bearbeiten");
		bearbeitenButton.addActionListener(controller);
		GridBagConstraints gbc_bearbeitenButton = new GridBagConstraints();
		gbc_bearbeitenButton.fill = GridBagConstraints.HORIZONTAL;
		gbc_bearbeitenButton.insets = new Insets(0, 0, 0, 5);
		gbc_bearbeitenButton.gridx = 3;
		gbc_bearbeitenButton.gridy = 0;
		buttonPanel.add(bearbeitenButton, gbc_bearbeitenButton);

		btnDrucken = new JButton("Drucken");
		btnDrucken.addActionListener(controller);
		GridBagConstraints gbc_btnDrucken = new GridBagConstraints();
		gbc_btnDrucken.fill = GridBagConstraints.HORIZONTAL;
		gbc_btnDrucken.gridx = 4;
		gbc_btnDrucken.gridy = 0;
		buttonPanel.add(btnDrucken, gbc_btnDrucken);
	}

	public JTable getDataSetTable() {

		if (dataSetTable == null) {
			dataSetTable = new JTable();
			dataSetTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
			dataSetTable.setModel(tableModel);
			dataSetTable.setAutoCreateRowSorter(true);
			final TableCellRenderer renderer = dataSetTable.getDefaultRenderer(Object.class);
			dataSetTable.setDefaultRenderer(PruefungAnzeigen.class, new PruefungCellRenderer(renderer));
			dataSetTable.setDefaultRenderer(String.class, new EinsatzstatusCellRenderer(renderer));
			dataSetTable.setDefaultRenderer(DataSet.class, new DataSetCellRenderer(renderer));
		}
		return dataSetTable;
	}

	public JTextField getFilterTextField() {

		return filterTextField;
	}

	public JComboBox<Gruppe> getGruppenComboBox() {

		if (gruppenComboBox == null) {

			gruppenComboBox = new JComboBox<Gruppe>();
			gruppenComboBox.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {

					tableModel.aktualisiere();
				}
			});
		}
		return gruppenComboBox;
	}

	public JButton getCommitButton() {

		return commitButton;
	}

	public JButton getDelButton() {

		return delButton;
	}

	public JButton getNeuButton() {

		return neuButton;
	}

	public JButton getBearbeitenButton() {

		return bearbeitenButton;
	}

	public JComboBox<JCheckBox> getTypeComboBox() {

		if (typeComboBox == null) {
			typeComboBox = new JComboCheckBox<JCheckBox>();
			DefaultComboBoxModel<JCheckBox> model = new DefaultComboBoxModel<>();
			for (Object s : DataSet.getMoeglicheTypen()) {
				model.addElement(new JCheckBox(s.toString(), true));
			}
			typeComboBox.setModel(model);
		}
		return typeComboBox;
	}

	public JButton getBtnDrucken() {
		return btnDrucken;
	}
}
